    package cn.javasea.graphql.graphql;

import cn.javasea.graphql.common.vo.PageInfo;
import cn.javasea.graphql.entity.Ad;
import cn.javasea.graphql.service.AdService;
import cn.javasea.graphql.vo.ad.index.IndexAdResult;
import cn.javasea.graphql.vo.ad.index.IndexAdResultData;
import graphql.schema.DataFetchingEnvironment;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

@Component
public class AdDataFetcher implements MyDataFetcher {

    @Autowired
    private AdService adService;

    @Override
    public String fieldName() {
        return "IndexAdList";
    }

    @Override
    public Object dataFetcher(DataFetchingEnvironment environment) {
        PageInfo<Ad> pageInfo = this.adService.queryAdList(1, 1, 3);
        List<Ad> ads = pageInfo.getRecords();

        List<IndexAdResultData> list = new ArrayList<>();
        for (Ad ad : ads) {
            list.add(new IndexAdResultData(ad.getId()+"", ad.getTitle(), ad.getType()+"", ad.getUrl()));
        }

        return new IndexAdResult(list);
    }
}
